/*
 * CVS file status:
 * 
 * $Id: LanguageManager.java,v 1.6 2011/01/25 10:29:45 lvh Exp $
 * 
 * Copyright (c) Smals
 */
package be.dch.gui.i18n;

import java.util.Enumeration;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

/**
 * This class will handles the internationalisation aspects
 * 
 * @author ehd3
 * 
 * @since 1.0
 * 
 */
public final class LanguageManager {

    private static final String I18N_BUNDLE = "be.dch.gui.i18n.garminmapgui";

    private static final Logger LOG = Logger.getLogger(LanguageManager.class);

    private static Locale locale = Locale.getDefault();

    private static ResourceBundle i18nBundle = ResourceBundle.getBundle(LanguageManager.I18N_BUNDLE, LanguageManager.locale, new UTF8Control());

    private LanguageManager() {
	// Utility class no constructor needed.
    }

    /**
     * initialise the resource bundle for the given locale/language
     * 
     * @param inLocale
     *            language name to initialise
     */
    public static void initialize(final Locale inLocale) {
	LanguageManager.logDebug("Initializing language to " + inLocale.toString());
	LanguageManager.locale = inLocale;

	try {
	    LanguageManager.i18nBundle = ResourceBundle.getBundle(LanguageManager.I18N_BUNDLE, inLocale);
	    LanguageManager.logDebug("Language set to " + inLocale.toString());
	    final Enumeration<String> keys = LanguageManager.i18nBundle.getKeys();
	    while (keys.hasMoreElements()) {
		final String key = keys.nextElement();
		LanguageManager.logDebug("[" + key + "] has value: " + LanguageManager.i18nBundle.getString(key));
	    }

	} catch (final MissingResourceException e) {
	    LanguageManager.LOG.error("Cannot set language to " + inLocale.toString());
	}
    }

    /**
     * Get property value from the I18N properties file.
     * 
     * @param key
     *            property to fetch from resourceBundle
     * @return property value when found. If key does not exists, the key is
     *         returned.
     */
    public static String translate(final String key) {
	String result = key;
	try {
	    result = LanguageManager.i18nBundle.getString(key);
	} catch (final Exception e) {
	    LanguageManager.LOG.warn(e.getMessage());
	}
	return result;

    }

    /**
     * @param exception
     * @return translated string class name of the exception
     */
    public static String translate(final Exception except) {
	String result = null;
	try {
	    final String key = LanguageManager.getShortClassName(except.getClass());
	    result = LanguageManager.i18nBundle.getString(key);
	} catch (final Exception exception) {
	    LanguageManager.LOG.warn(exception.getMessage());
	}
	return result;
    }

    /**
     * get class name from the fully qualified name.
     * 
     * @param clazz
     *            get class name
     * @return class name
     */
    @SuppressWarnings("rawtypes")
    public static String getShortClassName(final Class clazz) {
	return clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1);
    }

    /**
     * @return the locale
     */
    public static Locale getLocale() {
	return LanguageManager.locale;
    }

    private static void logDebug(final String message) {
	if (LanguageManager.LOG.isDebugEnabled()) {
	    LanguageManager.LOG.debug(message);
	}
    }

}
